<?php

/**
* This method unzips a directory within a zip-archive
*
* @author Florian 'x!sign.dll' Wolf
* @license LGPL v2 or later
* @link http://www.xsigndll.de
* @link http://www.clansuite.com
*/

function extractZip( $zipFile = '', $dirFromZip = '' )
{
    define(DIRECTORY_SEPARATOR, '/');

    $zipDir = getcwd() . DIRECTORY_SEPARATOR;
    $zip = zip_open($zipDir.$zipFile);

    if ($zip)
    {
        while ($zip_entry = zip_read($zip))
        {
            $completePath = $zipDir . dirname(zip_entry_name($zip_entry));
            $completeName = $zipDir . zip_entry_name($zip_entry);

            // Walk through path to create non existing directories
            // This won't apply to empty directories ! They are created further below
            if(!file_exists($completePath) && preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
            {
                $tmp = '';
                foreach(explode('/',$completePath) AS $k)
                {
                    $tmp .= $k.'/';
                    if(!file_exists($tmp) )
                    {
                        @mkdir($tmp, 0777);
                    }
                }
            }

            if (zip_entry_open($zip, $zip_entry, "r"))
            {
                if( preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
                {
                    if ($fd = @fopen($completeName, 'w+'))
                    {
                        fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                        fclose($fd);
                    }
                    else
                    {
                        // We think this was an empty directory
                        mkdir($completeName, 0777);
                    }
                    zip_entry_close($zip_entry);
                }
            }
        }
        zip_close($zip);
    }
    return true;
}

// The call to exctract a path within the zip file

// The unzip script
    // Created by Alex at http://www.learncpp.com
    //
    // This script lists all of the .zip files in a directory
    // and allows you to select one to unzip.  Unlike CPanel's file
    // manager, it _will_ overwrite existing files.
    //
    // To use this script, FTP or paste this script into a file in the directory
    // with the .zip you want to unzip.  Then point your web browser at this
    // script and choose which file to unzip.

    // See if there's a file parameter in the URL string
    $file = $_GET['file'];

    if (isset($file))
    {
       echo "Unzipping " . $file . "<br>";

        extractZip( $file, './pacts' );


//        system('unzip -o ' . $file);
       exit;
    }

    // create a handler to read the directory contents
    $handler = opendir(".");

    echo "Please choose a file to unzip: " . "<br>";

    // A blank action field posts the form to itself
    echo '<FORM action="" method="get">';

    $found = FALSE; // Used to see if there were any valid files

    // keep going until all files in directory have been read
    while ($file = readdir($handler))
    {
        if (preg_match ("/.zip$/i", $file))
        {
            echo '<input type="radio" name="file" value=' . $file . '> ' . $file . '<br>';
            $found = true;
        }
    }

    closedir($handler);

    if ($found == FALSE)
        echo "No files ending in .zip found<br>";
    else
        echo '<br>Warning: Existing files will be overwritten.<br><br><INPUT type="submit" value="Unzip!">';

    echo "</FORM>";
?>
